December 5, 2017
DON'T DO THIS!!
Biggest reason for good practices is
YOUR OWN SANITY
When you create a Project, the following obvious things happen:
The following not-so-obvious things happen:
I use Projects so that:
I always work with RStudio Projects to encapsulate my projects.
However, each project needs to maintain a file structure to know where to find things
You can certainly choose your own organization for files
I have a suggested way:
# install.packages('devtools')
devtools::install_github('webbedfeet/ProjTemplate')
ProjTemplate::useTemplate()
Notebook.RmdYou can load different packages in different scripts
Then you run a particular script and realize it needed a package you loaded in another script
You didn't realize it because the packae was already loaded when you developed the script
So I started putting all the package calls in a single place, in 'lib/pkgs.yml'
What's this pkgs.yml?!!!
The name pkgs.yml is specific if you use my ProjTemplate pkg, which expects a lib/pkgs.yml
You can also start it using, for example, ProjTemplate::add_package(c('tidyverse','ggplot2'))
You can load all the packages using ProjTemplate::load_packages(). This will install packages if they're not already installed, and then load them into the current namespace
runif(n = nrow(dat), min = min(dat$age), max = max(dat$age))
rather than
runif(n = 135, min = 18, max = 80)
lib/R.If you're using ProjTemplate:
lib/R sub-directory of your main directoryProjTemplate::reload() to load all the functions into a separate environment
reload will also load packages at the same time.Otherwise:
funcfiles <- dir('lib/R', pattern = '.R')
for(f in funcfiles){
source(f)
}
The directory structure created and the way files are stored makes the lib folder easily convertible into an R package.
It has almost the right structure, just needs to become a package
See Hadley's book "R Packages", available for FREE here
You can also create a package structure using RStudio Projects; just choose "R Package" instead of "New Project". It gets you exactly the right file structure
Raw data >> Intermediate data >> Final data >> data for sub-analyses >> data for final tables and figures
David Robinson, 2016
FinalTables.R and FinalFigures.R. Duh!This provides final check that right data are used, and can be updated easily during revision cycle
It's a long road to this point, so make sure things are good.
The only way to learn R is to use R
Send talks to your instructor before class starts